package com.lcb.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcb.blog.base.BaseApiService;
import com.lcb.blog.base.BaseResponse;
import com.lcb.blog.entity.Module;
import com.lcb.blog.mapper.ModuleMapper;
import com.lcb.blog.service.ModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;

/****
 * @Author:lcb
 * @Description:Module业务层接口实现类
 * @Date: 2020-12-29 14:38:32
 *****/
@Api(tags = "ModuleServiceImpl")
@RestController
//@RequestMapping("/blog")
//@CrossOrigin
public class ModuleServiceImpl extends BaseApiService implements ModuleService {

    @Autowired
    private ModuleMapper moduleMapper;

    /**
     * Module条件+分页查询
     *
     * @param module 查询条件
     * @param page   页码
     * @param size   页大小
     * @return 分页结果
     */
    @ApiOperation(value = "Module条件查询", notes = "条件查询Module方法详情", tags = {"ModuleServiceImpl"})
    @Override
    public BaseResponse findModuleList(@RequestBody Module module, @PathVariable(value = "page") int page, @PathVariable(value = "size") int size) {
        //分页
        PageHelper.startPage(page, size);

        PageInfo<Module> pagelist = new PageInfo<Module>(moduleMapper.select(module));

        return setResultSuccess(pagelist);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @ApiOperation(value = "Module根据ID删除", notes = "根据ID删除Module方法详情", tags = {"ModuleServiceImpl"})
    @Override
    public BaseResponse deleteModuleById(@PathVariable(value = "ids") String ids) {

        String[] split = ids.split(",");
        Example example = new Example(Module.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(split));
        moduleMapper.deleteByExample(example);
        return setResultSuccess("删除成功");
    }

    /**
     * 修改Module
     *
     * @param module
     */
    @ApiOperation(value = "Module根据ID修改", notes = "根据ID修改Module方法详情", tags = {"ModuleServiceImpl"})
    @Override
    public BaseResponse updateModule(@RequestBody Module module) {
        moduleMapper.updateByPrimaryKeySelective(module);
        return setResultSuccess("修改成功");
    }

    /**
     * 增加Module
     *
     * @param module
     */
    @ApiOperation(value = "Module添加", notes = "添加Module方法详情", tags = {"ModuleServiceImpl"})
    @Override
    public BaseResponse addModule(@RequestBody Module module) {
        moduleMapper.insertSelective(module);
        return setResultSuccess("添加成功");
    }

    /**
     * 根据ID查询Module
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "Module根据ID查询", notes = "根据ID查询Module方法详情", tags = {"ModuleServiceImpl"})
    @Override
    public BaseResponse findModuleById(@PathVariable(value = "id") Integer id) {
        return setResultSuccess(moduleMapper.selectByPrimaryKey(id));
    }

    /**
     * 查询Module全部数据
     *
     * @return
     */
    @ApiOperation(value = "查询所有Module", notes = "查询Module所有方法详情", tags = {"ModuleServiceImpl"})
    @Override
    public BaseResponse findModuleAll() {
        return setResultSuccess(moduleMapper.selectAll());
    }
}
